import re
import time
time.sleep(5)
import urllib.request
import pandas


id = []
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
newsID = open('D://nanfang/20200404/id.txt','w',encoding='utf-8')
try:
    for i in range(1,10):
        url ='http://www.infzm.com/search?term_id=&page='+str(i)+'&k=%E7%96%AB%E6%83%85'
        html = urllib.request.Request(url=url,headers=headers)
        data = urllib.request.urlopen(html).read().decode('utf-8')
        pat = '<a href="/contents/(\d+)">'
        news_id = re.compile(pat).findall(data)
        for t in news_id:
            id.append(t+'\n')
            print(len(id))
        for r in id:
            newsID.write(r)
except Exception as err:
    print(err)